package com.example.demo.controller;

import com.example.demo.config.AESUtil;
import com.example.demo.config.Result;
import com.example.demo.entity.user.InsertUser;
import com.example.demo.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Register API", description = "注册管理")
@RestController
@RequestMapping("/register")
public class RegisterController {
    @Autowired
    private UserService userService;

    @Operation(summary = "注册用户信息", tags = "Register API")
    @RequestMapping(value = "registerUser", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    public Result registerUser(@RequestBody InsertUser insertUser) throws Exception {
        if (insertUser.getName() == null || insertUser.getName().trim().isEmpty() || insertUser.getPassword() == null
                || insertUser.getPassword().trim().isEmpty()) {
            return Result.paramError("用户名或密码不能为空");
        }
        try {
            String mdCode = AESUtil.decrypt(insertUser.getPassword());
            insertUser.setPassword(AESUtil.mdEncryption(mdCode));
            insertUser.setType(1);
            int num = userService.addUser(insertUser);
            return Result.success(num);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }

    }
}
